<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>权限管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="权限名称/权限" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                <el-button @click="_findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="权限名称" align="center"></el-table-column>
                <el-table-column prop="keyword" label="权限" align="center"></el-table-column>
                <el-table-column prop="description" label="权限说明" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增权限" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="权限名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="权限" prop="keyword">
                                    <el-input v-model="formData.keyword"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="权限说明">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="权限名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="权限" prop="keyword">
                                    <el-input v-model="formData.keyword"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="权限说明">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data:{
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:10,//每页显示的记录数
                total:0,//总记录数
                queryString:null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见
            rules: {//校验规则
                name: [{ required: true, message: '权限名称为必填项', trigger: 'blur' }],
                keyword: [{ required: true, message: '权限为必填项', trigger: 'blur' }]
            },
            backendUrl:"http://localhost:82"
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this._findPage();
        },
        methods: {
            //编辑
            //1. 数据校验，如果校验成功，继续，如果失败，提示信息
            //2. 发送请求，编写后端
            //3. 成功回调后：关闭弹框，重新加载，重置表单
            handleEdit() {
                //1. 数据校验，如果校验成功，继续，如果失败，提示信息
                this.$refs['dataEditForm'].validate((valid)=>{
                    if (valid) {
                        //为true,有数据，发送请求。
                        axios.post(this.backendUrl+"/permission/edit.do",this.formData).then((response)=>{
                            if (response.data.flag) {
                                //为true，修改成功.
                                this.$message({
                                    message:response.data.message,
                                    type:"success"
                                });
                            }else {
                                this.$message({
                                    message:response.data.message,
                                    type:"error"
                                });
                            }
                            this.dialogFormVisible4Edit=false;
                            this.resetForm();
                            this._findPage();
                        }).catch((err)=>{
                            this.showMessage(err)
                        });
                    }
                })
            },
            //添加
            //1. 填充数据，数据校验
            //2. 如果校验成功，发送请求到后端;如果失败，提示错误信息
            //3. 返回结果，如果失败，提示失败，如果成功，提示成功
            //4. 关闭弹框
            //5. 重新查询
            //6. 重置表单
            handleAdd () {
                //1. 填充数据，数据校验
                this.$refs['dataAddForm'].validate((valid)=>{
                    //2. 如果校验成功，发送请求到后端;如果失败，提示错误信息
                    //valid：校验结果。true、false。
                    if(valid){
                        //3. 返回结果，如果失败，提示失败，如果成功，提示成功
                        //发送请求。
                        axios.post(this.backendUrl+"/permission/add.do",this.formData).then((response)=>{
                            console.log(response);
                            //3. 返回结果，如果失败，提示失败，如果成功，提示成功
                            if (response.data.flag){
                                this.$message({
                                    message: response.data.message,
                                    type: "success" // info ,error, success, warning
                                });
                            }else {
                                this.$message({
                                    message: response.data.message,
                                    type: "error" // info ,error, success, warning
                                });
                            }
                            //4. 关闭弹框
                            this.dialogFormVisible=false;
                            //5. 重新查询
                            this._findPage();
                            //6. 重置表单
                            this.resetForm();
                        }).catch((err)=>{
                            this.showMessage(err)
                        });
                    }

                });
            },
            _findPage(){
                if(this.pagination.queryString!= null && this.pagination.queryString.length > 0){
                    //如果条件不为null，则查询第一页
                    this.pagination.currentPage = 1;
                }
                this.findPage();
            },
            //1. 分装分页参数
            //2. 发送请求，查询数据
            //3. 列表数据需要在表格中展示， 分页组件实现
            //分页查询
            findPage() {
                //1. 分装分页参数
                var param={
                    currentPage: this.pagination.currentPage,
                    pageSize:this.pagination.pageSize,//每页显示的记录数
                    queryString:this.pagination.queryString//查询条件
                };
                //2. 发送请求，查询数据
                axios.post(this.backendUrl+"/permission/findPage.do",param).then((response)=>{
                    //3. 列表数据需要在表格中展示， 分页组件实现
                    this.dataList=response.data.rows;
                    this.pagination.total=response.data.total;
                }).catch((err)=>{
                    this.showMessage(err)
                });
            },
            // 重置表单
            resetForm() {
                //对表单数据进行清空
                this.formData={};
                if (this.$refs['dataAddForm'] != null) {
                    this.$refs['dataAddForm'].resetFields();
                }
            },
            // 弹出添加窗口
            handleCreate() {
                //重置表单
                this.resetForm();
                //弹出窗口
                this.dialogFormVisible=true;
            },
            //1. 弹出窗口
            //2. 重置表单
            //3. 根据id回显数据
            // 弹出编辑窗口
            handleUpdate(row) {
                //1. 弹出窗口
                this.dialogFormVisible4Edit=true;
                //2. 重置表单
                this.resetForm();
                //3. 根据id回显数据
                axios.get(this.backendUrl+"/permission/findById.do?id="+row.id).then((response)=>{
                    //查询成功后，回显
                    this.formData=response.data.data;
                }).catch((err)=>{
                    this.showMessage(err)
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage=currentPage;
                this._findPage();
            },
            // 删除
            handleDelete(row) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //点击确认按钮要执行的操作： 删除操作
                    axios.get(this.backendUrl+"/permission/delById.do?id="+row.id).then((response)=>{
                        if (response.data.flag) {
                            //删除成功
                            this.$message({
                                type:"success",
                                message:response.data.message
                            });
                        }else {
                            //删除失败
                            this.$message({
                                type:"error",
                                message:response.data.message
                            });
                        }
                        //重新查询
                        this._findPage();
                    }).catch((err)=>{
                        this.showMessage(err)
                    });
                }).catch(()=>{
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            showMessage(err){
                console.log(err);
                if(err == 'Error: Request failed with status code 403'){
                    this.$message({
                        type:"error",
                        message:"权限不足，切换角色！！"
                    });
                }else{
                    this.$message({
                        type:"error",
                        message:"未知错误，请联系管理员！！"
                    });
                }
            }
        }
    })
</script>

</html>